<?
require_once('common.php');
require_once('../bin/db.php');

function getBilling($view,$region,$carrier){
	global $myDB;
	$sum 	= 0;
	$weight = 0;
	for ($i=0;$i<count($view);$i++) {
		$sum	+= $view[$i][4]*$view[$i][5];
		$weight += $view[$i][3]*$view[$i][5];
	}

	if ($carrier=='Self Take') $billing[1]=0;
	else {
		$sql="select charge,weightto from med_shipping where region='$region' and carrier='$carrier' order by weightto desc limit 0,1";
		$myDB->Query($sql);
		$rec		= mysql_fetch_object($myDB->result);
		$maxcharge	= $rec->charge;
		$maxweight	= $rec->weightto;
		if ($weight<=$maxweight) { 
			$sql="select charge from med_shipping where region='$region' and carrier='$carrier' and $weight >= weightfrom and $weight <= weightto;";
			$myDB->Query($sql);
			$rec=mysql_fetch_object($myDB->result);
			$billing[1]	= $rec->charge;
		}
		else {
			$billing[1]	= @floor($weight/$maxweight)*$maxcharge;
			$subweight	= $weight - $maxweight * @floor($weight/$maxweight);
			$sql="select charge from med_shipping where region='$region' and carrier='$carrier' and $subweight >= weightfrom and $subweight <= weightto;";
			$myDB->Query($sql);
			$rec=mysql_fetch_object($myDB->result);
			$billing[1]	+= $rec->charge;
		}
	}
	

	if (strpos(strtoupper($region),'HONG KONG')===false && $carrier=='Express') {
			$sql="select value from misc_data where name='express'";
			$myDB->Query($sql);
			$rec=mysql_fetch_object($myDB->result);
			$rate=$rec->value;
			$billing[1]=round($billing[1]*(100+$rate)/100,2);
	}

	if ($carrier=='Claim') {
			$sql="select cid from med_shipping where region='$region'";
			$myDB->Query($sql);
			$rec=mysql_fetch_object($myDB->result);
			$cid	= $rec->cid;
			$billing[3]	= $cid;

			$sql="select rank from station where id='$cid'";
			$myDB->Query($sql);
			$rec=mysql_fetch_object($myDB->result);
			$rank = $rec->rank;
			
		$billing[1]	= $billing[1]+$sum*$rank/100;
	} 
	
	$billing[0]	= $sum;
	$billing[2]	= $sum + $billing[1];
	
	
	return $billing;

}//:~end of function getBilling()

$id=$_GET['id'];
$no=$_GET['no'];
$myDB= new MySQL_Class();
$myDB->Create();

//update stock
	$sql="select c.pro_name,c.pro_code,c.qty,c.unit_price,b.id,b.weight from cus_order_sub as c,pro_basic as b where b.code=c.pro_code and c.no=$no";
	$myDB->Query($sql);
	if ($res=$myDB->result) {
		$i=0;
		while ($rec=mysql_fetch_object($res)){
			$myview[$i][0]	= $rec->id;
			$myview[$i][1]	= $rec->pro_name;
			$myview[$i][2]	= $rec->pro_code;
			$myview[$i][3]	= $rec->weight;
			$myview[$i][4]	= $rec->unit_price;
			$myview[$i][5]	= $rec->qty;
			$i++;
		}
	}

//mail e-recipient
		$sql="select * from cus_order where id=$id";
		$myDB->Query($sql);
		$rec=mysql_fetch_object($myDB->result);
		$html='';
		for ($i=0;$i<count($myview);$i++)
			$html.='<tr><td>['.$myview[$i][2].'] '.$myview[$i][1].'</td><td>'.$myview[$i][5].'</td><td align="right">'.number_format($myview[$i][4],2).'</td><td align="right">'.number_format(($myview[$i][4]*$myview[$i][5]),2).'</td></tr>';
		$charge=getBilling($myview,$rec->shipping_region,$rec->shipping_carrier);


		$html='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Jonesky</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<style type="text/css">
body {font-size: 10pt; font-family: Arial }
td {font-size: 10pt; font-family: Arial }
</style>
</head>
<body bgcolor="#FFFFFF">
<table width="600" border="0" cellspacing="0" cellpadding="1">
  <tr>
    <td align="right"><table width="180" border="0" cellpadding="1" cellspacing="0">
  <tr>
    <td bgcolor="#000000"><table width="100%" border="0" cellpadding="1" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
                <td align="center"><font size="3" face="Verdana, Arial, Helvetica, sans-serif"><strong>Jonesky  (H.K.)
                  Limited </strong></font></td>
        </tr>
      </table></td>
  </tr>
</table></td>
  </tr>
</table>
<br>
<font size="2" face="Arial, Helvetica, sans-serif"><img src="http://jonesky.imaker.com.hk/pictures/e-receipt.jpg?'.rand(0, 9999).'"><br>
Dear <font color="#0000FF">'.$rec->member_name.'</font>, </font><font face="Arial, Helvetica, sans-serif"> 
<p><font size="2">Thank you for your order.<br>
  This email confirms receipt of your order<font color="#0000FF"> '.str_pad($rec->no,5,'0',STR_PAD_LEFT).'</font> 
  taken on <font color="#0000FF">'.date('Y-m-d H:i:s',$rec->purchase_date).'</font></font></p>
<p><font size="2">The order consists of the following:</font></p>
</font> 
<table width="600" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td width="341"><strong><font size="2" face="Arial, Helvetica, sans-serif">Item</font></strong></td>
    <td width="57"><div align="center"><strong><font size="2" face="Arial, Helvetica, sans-serif">Qty</font></strong></div></td>
    <td width="120"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Price</strong></font></div></td>
    <td width="82"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Amount</strong></font></div></td>
  </tr>
  <tr> 
    <td colspan="4"> <hr size="1"> </td>
  </tr>
  <tr> 
    <td><font size="2" face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
    <td><font size="2" face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
    <td><div align="right"><strong><font size="1" face="Arial, Helvetica, sans-serif">HKD</font></strong></div></td>
    <td><div align="right"><strong><font size="1" face="Arial, Helvetica, sans-serif">HKD</font></strong></div></td>
  </tr>'.$html.'
  <tr> 
    <td colspan="4"> <hr size="1"> </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><div align="right"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Sub 
        Total :</strong></font></div></td>
    <td align="right">'.number_format($charge[0],2).'</td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><div align="right"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Shipping 
        Charge :</strong></font></div></td>
    <td>'.number_format($charge[1],2).'</td>
  </tr>
  <tr> 
    <td><font size="2" face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
    <td><font size="2" face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
    <td><div align="right"><strong><font size="2" face="Arial, Helvetica, sans-serif">Total 
        :</font></strong></div></td>
    <td><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">'.number_format($charge[2],2).'</font></div></td>
  </tr>
</table>
<br>
<br>
<table width="601" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><strong><font size="2" face="Arial, Helvetica, sans-serif">Delivery Region / Method :</font></strong><br>
      <font color="#0000FF" size="2" face="Arial, Helvetica, sans-serif">'.$rec->shipping_region.' , '.$rec->shipping_carrier.'</font></td>
  </tr>
  <tr> 
    <td><hr size="1"></td>
  </tr>
  <tr> 
    <td width="601"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Member Registration Address :</strong><br>
      <font color="#0000FF">'.nl2br($rec->recipient_address).'</font></td>
  </tr>
  <tr> 
    <td><hr size="1"></td>
  </tr>
  <tr> 
    <td><font size="2" face="Arial, Helvetica, sans-serif"><strong>Delivery Address :</strong><br>
      <font color="#0000FF">'.nl2br($rec->billing_address).'</font></td>
  </tr>
  <tr> 
    <td height="24"><hr size="1"></td>
  </tr>
  <tr> 
    <td height="22"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Payment 
      received by :<br>
      </strong><font color="#0000FF">'.$rec->pay_method.'</font><br>
  </tr>
  <tr> 
    <td><br>
      <br></td>
  </tr>
  <tr> 
    <td><font face="Arial, Helvetica, sans-serif"><font size="2"><br>
      <font face="Arial, Helvetica, sans-serif"> If you want to get more, please 
      visit <a href="http://jonesky.imaker.com.hk%20">our website</a>.</font> 
      </font><font size="2" face="Arial, Helvetica, sans-serif"> </font></font></td>
  </tr>
</table>
<p>&nbsp;</p><p><font size="2" face="Arial, Helvetica, sans-serif"><strong>Jonesky Limited</strong><br>
  Unit 1201, 1205-8, 12/F Westlands Centre,<br>
  20 Westlands Road<br>
  Quarry Bay<br>
  Hong Kong<br>
  Tel: 2561-8172<br>
  Fax:2565-9326<br>
  <a href="http://www.comicsworld.com%20">www.comicsworld.com </a></font></p>
<p>&nbsp;</p>
<p>&nbsp; </p>
</body>
</html>';

	$tomail='"'.$rec->member_name.'" <'.$rec->member_email.'>';
/*	$sql="select name,email from user_profile where alert03='y'";
	$myDB->Query($sql);
	$i=1;
	if ($res=$myDB->result) {
		while ($rec=mysql_fetch_object($res)) {
			$tomail[$i]='"'.$rec->name.'" <'.$rec->email.'>';
		}
	}

		for ($i=0;$i<count($tomail);$i++)
*/			if (myMail('Thank you for your order <Resend>',$html,'',$tomail))
				$msg= "e-Receipt has been resent to ".$tomail." ! <br>";
			else $msg= "Failed to resend e-Receipt to ".$tomail." ! <br>"


?>
<html>
<head>
<title>Resend e-Receipt</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5"><style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
.font {
	font-family: "Arial";
	font-size: 14px;
}
-->
</style></head>
<body><br>
<br>
<br>
<div align="center" class="font"><?=$msg?>
  <br>
  <br>
  <br>
  <input name="Submit" type="button" class="font" onClick="window.close();return true;" value="Close">
</div>
</body>
</html>